<!DOCTYPE html>
<html lang="<?php echo $pageConfig['language']; ?>" dir="<?php echo $pageConfig['direction']; ?>">
<head>
    <meta charset="<?php echo $pageConfig['charset']; ?>">
    <meta name="viewport" content="<?php echo $pageConfig['viewport']; ?>">
    <meta name="description" content="<?php echo $pageConfig['description'] ?? ''; ?>">
    <meta name="keywords" content="<?php echo $pageConfig['keywords'] ?? ''; ?>">
    <meta name="generator" content="<?php echo $pageConfig['generator']; ?>">
    <meta name="author" content="<?php echo $pageConfig['author'] ?? ''; ?>">
    
    <!-- 社交媒体元标签 -->
    <meta property="og:title" content="<?php echo $pageConfig['title']; ?>">
    <meta property="og:description" content="<?php echo $pageConfig['description']; ?>">
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?php echo $pageConfig['url'] ?? ''; ?>">
    <?php if (!empty($pageConfig['image'])): ?>
        <meta property="og:image" content="<?php echo $pageConfig['image']; ?>">
    <?php endif; ?>
    
    <!-- 标题 -->
    <title><?php echo $pageConfig['title']; ?></title>
    
    <!-- 图标 -->
    <link rel="icon" href="<?php echo $pageConfig['favicon']; ?>">
    <link rel="apple-touch-icon" href="<?php echo $pageConfig['apple_touch_icon'] ?? $pageConfig['favicon']; ?>">
    
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- 额外的CSS库 -->
    <?php if (!empty($pageConfig['extra_css'])): ?>
        <?php foreach ($pageConfig['extra_css'] as $cssLib): ?>
            <link rel="stylesheet" href="<?php echo $cssLib; ?>">
        <?php endforeach; ?>
    <?php endif; ?>
    
    <!-- 自定义Tailwind配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: <?php echo json_encode($themeConfig ?? []);?>
            }
        }
    </script>
    
    <!-- 自定义工具类 -->
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .transition-custom {
                transition: all 0.3s ease;
            }
            <?php if (!empty($pageConfig['custom_utilities'])): ?>
                <?php echo $pageConfig['custom_utilities']; ?>
            <?php endif; ?>
        }
    </style>
    
    <!-- 全局CSS -->
    <link rel="stylesheet" href="<?php echo ASSETS_PATH; ?>css/global.css">
    
    <!-- 页面额外CSS -->
    <?php if (!empty($pageConfig['extra_css'])): ?>
        <?php foreach ($pageConfig['extra_css'] as $cssFile): ?>
            <link rel="stylesheet" href="<?php echo $cssFile; ?>">
        <?php endforeach; ?>
    <?php endif; ?>
</head>
<body class="<?php echo $pageConfig['body_class']; ?>">
    <?php
    // 引入头部导航 - 使用映射表查找文件
    $headerFile = FRAGMENTS_PATH . ($fragmentMap['header'] ?? 'common/header.php');
    if (file_exists($headerFile)) {
        require $headerFile;
    }
    ?>
    <?php
    if(isset($pageConfig['components']['hero'])){
        call_user_func('renderComponent', 'hero');
    }
    ?>
    <main class="<?php echo $pageConfig['base_class']; ?>" style="<?php echo $pageConfig['base_style']; ?>">
        <!-- 引入页面依赖的碎片 -->
        <?php if (!empty($pageConfig['dependencies'])): ?>
            <?php foreach ($pageConfig['dependencies'] as $fragment): ?>
                <?php
                // 使用渲染组件函数
                if (is_callable('renderComponent')) {
                    call_user_func('renderComponent', $fragment);
                } else {
                    // 回退方案：使用映射表查找文件路径
                    $fragmentPath = isset($fragmentMap[$fragment]) ? $fragmentMap[$fragment] : ($fragment . '.php');
                    $fragmentFile = FRAGMENTS_PATH . $fragmentPath;
                    if (file_exists($fragmentFile)) {
                        $fragmentVar = $fragment;
                        require $fragmentFile;
                    }
                }
                ?>
            <?php endforeach; ?>
        <?php endif; ?>
    </main>

    <?php
    // 引入页脚 - 使用映射表查找文件
    $footerFile = FRAGMENTS_PATH . ($fragmentMap['footer'] ?? 'common/footer.php');
    if (file_exists($footerFile)) {
        require $footerFile;
    }
    ?>
    <?php if (!empty($pageConfig['extra_js_libs'])): ?>
        <?php foreach ($pageConfig['extra_js_libs'] as $jsLib): ?>
            <script src="<?php echo $jsLib; ?>"></script>
        <?php endforeach; ?>
    <?php endif; ?>
    
    <!-- 页面额外JS -->
    <?php if (!empty($pageConfig['extra_js'])): ?>
        <?php foreach ($pageConfig['extra_js'] as $jsFile): ?>
            <script src="<?php echo $jsFile; ?>"></script>
        <?php endforeach; ?>
    <?php endif; ?>
    
    <!-- 全局脚本 -->
    <script src="<?php echo ASSETS_PATH; ?>js/global.js"></script>
    
    <!-- 引入脚本 - 使用映射表查找文件 -->
    <?php
    $scriptsFile = FRAGMENTS_PATH . ($fragmentMap['scripts'] ?? 'common/scripts.php');
    if (file_exists($scriptsFile)) {
        require $scriptsFile;
    }
    ?>
    
    <!-- 分析代码 -->
    <?php if (!empty($pageConfig['analytics'])): ?>
        <?php echo $pageConfig['analytics']; ?>
    <?php endif; ?>
</body>
</html>